﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using VNet.VisiSuite.Controls;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.VisiCentral.Common.Helpers;

namespace VNet.VisiSuite.VisiCentral.Controls
{
    public partial class Header2Control : UserControl
    {
        #region Dependencies

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(Header2Control), new FrameworkPropertyMetadata(string.Empty, TitleChangedCallback));
        public static readonly DependencyProperty LeftMenuIconsProperty = DependencyProperty.Register("LeftMenuIcons", typeof(IList<VNetMenuItem>), typeof(Header2Control), new FrameworkPropertyMetadata(null, LeftMenuIconsChangedCallback));
        public static readonly DependencyProperty RightMenuIconsProperty = DependencyProperty.Register("RightMenuIcons", typeof(IList<VNetMenuItem>), typeof(Header2Control), new FrameworkPropertyMetadata(null, RightMenuIconsChangedCallback));

        public static readonly DependencyProperty ShiftDateProperty = DependencyProperty.Register("ShiftDate", typeof(DateTime), typeof(Header2Control), new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty ShiftProperty = DependencyProperty.Register("Shift", typeof(AreaShiftItem), typeof(Header2Control), new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty ShiftListProperty = DependencyProperty.Register("ShiftList", typeof(IList<AreaShiftItem>), typeof(Header2Control), new FrameworkPropertyMetadata(null));

        public static readonly DependencyProperty PreviousShiftCommandProperty = DependencyProperty.Register("PreviousShiftCommand", typeof(RelayCommand), typeof(Header2Control), new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty NextShiftCommandProperty = DependencyProperty.Register("NextShiftCommand", typeof(RelayCommand), typeof(Header2Control), new FrameworkPropertyMetadata(null));

        public static readonly DependencyProperty MaxShiftDateProperty = DependencyProperty.Register("MaxShiftDate", typeof(DateTime), typeof(Header2Control), new FrameworkPropertyMetadata(null));

        #endregion

        #region Private Variables

        private DispatcherTimer _timer;

        #endregion

        #region Constructors

        public Header2Control()
        {
            InitializeComponent();
            _titleTblk.DataContext = _rightMenu.DataContext = this;
            cmbShift.DataContext = this;
            _datetime_text.Text = DateTime.Now.ToString("d-MMM-yyyy h:mm tt");
            _timer = new DispatcherTimer(new TimeSpan(0, 1, 0), DispatcherPriority.Normal, delegate
            {
                _datetime_text.Text = DateTime.Now.ToString("d-MMM-yyyy h:mm tt");
            }, this.Dispatcher);
            _timer.Start();
        }

        #endregion

        #region Callbacks

        private static void LeftMenuIconsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Header2Control;
            control._leftMenu.Icons = (IList<VNetMenuItem>)e.NewValue;
        }

        private static void TitleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Header2Control;
            control._titleTblk.Text = (string)e.NewValue;
        }

        private static void RightMenuIconsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Header2Control;
            control._rightMenu.Icons = (IList<VNetMenuItem>)e.NewValue;
        }

        #endregion

        #region Properties

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public IList<VNetMenuItem> LeftMenuIcons
        {
            get { return (IList<VNetMenuItem>)GetValue(LeftMenuIconsProperty); }
            set { SetValue(LeftMenuIconsProperty, value); }
        }

        public IList<VNetMenuItem> RightMenuIcons
        {
            get { return (IList<VNetMenuItem>)GetValue(RightMenuIconsProperty); }
            set { SetValue(RightMenuIconsProperty, value); }
        }
        
        public IList<AreaShiftItem> ShiftList
        {
            get { return (IList<AreaShiftItem>)GetValue(ShiftListProperty); }
            set { SetValue(ShiftListProperty, value); }
        }

        public AreaShiftItem Shift
        {
            get { return (AreaShiftItem)GetValue(ShiftProperty); }
            set { SetValue(ShiftProperty, value); }
        }

        public DateTime ShiftDate
        {
            get { return (DateTime)GetValue(ShiftDateProperty); }
            set { SetValue(ShiftDateProperty, value); }
        }

        public DateTime MaxShiftDate
        {
            get { return (DateTime)GetValue(MaxShiftDateProperty); }
            set { SetValue(MaxShiftDateProperty, value); }
        }

        public RelayCommand NextShiftCommand
        {
            get { return (RelayCommand)GetValue(NextShiftCommandProperty); }
            set { SetValue(NextShiftCommandProperty, value); }
        }

        public RelayCommand PreviousShiftCommand
        {
            get { return (RelayCommand)GetValue(PreviousShiftCommandProperty); }
            set { SetValue(PreviousShiftCommandProperty, value); }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the SelectionChanged event of the cmbShift control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SelectionChangedEventArgs"/> instance containing the event data.</param>
        //private void cmbShift_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    //// Validate if selected shift is in the future
        //    //if (DateTime.Now.Date.Add(Shift.StartTime) > DateTime.Now)
        //    //{
        //    //    e.
        //    //}
        //    //Shift = cmbShift.SelectedItem as AreaShiftItem;
        //    e.Handled = true;
        //}

        private void OnUnloaded(object sender, RoutedEventArgs e)
        {
            _timer.Stop();
        }

        #endregion
    }
}
